package com.frame.database;

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import com.frame.metadata.wordbook.WordbookService;
import com.frame.util.DataConvert;
import com.frame.util.SelectItem;

/**
 * @author 王海锋
 * @version 1.0
 * @Title: 系统
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company:
 * @CreatedTime:2012-9-4 下午12:03:16
 */

public class JdataBean {

    private Map dataMap = new HashMap();

    public void put(String key, Object value) {
        dataMap.put(key.toLowerCase(), value);
    }

    public Object get(String key) {
        return dataMap.get(key.toLowerCase());
    }

    public Object get(String key, String format) {
        Object obj = dataMap.get(key.toLowerCase());
        if (obj != null) {
            return DataConvert.toString(obj, format);
        }
        return obj;
    }

    public String getByDict(String key, String dict) throws Exception {
        String val = getString(key);
        if (StringUtils.isNotBlank(dict)) {
            SelectItem item = WordbookService.getIntance().getItemName(dict, val);
            if (item != null)
                val = item.getText();
        }
        return val;
    }

    public String getByEnum(String key, Class enumClass, String enumKey, String enumValue) throws Exception {
        String val = getString(key);
        if (enumClass != null) {
            Enum[] enums = (Enum[]) enumClass.getEnumConstants();
            for (Enum enumb : enums) {
                String methodName = Character.toUpperCase(enumKey.charAt(0)) + enumKey.substring(1);
                Method getMethod = enumb.getClass().getMethod("get" + methodName, new Class[0]);
                Object obj = getMethod.invoke(enumb, new Object[0]);
                if (obj.toString().equals(val)) {
                    String methodNamev = Character.toUpperCase(enumValue.charAt(0)) + enumValue.substring(1);
                    Method getMethodv = enumb.getClass().getMethod("get" + methodNamev, new Class[0]);
                    Object objn = getMethodv.invoke(enumb, new Object[0]);
                    if (objn != null)
                        val = objn.toString();
                }

            }
        }
        return val;
    }

    public Object getDefault(String key, Object defaultobj) {
        Object obj = dataMap.get(key.toLowerCase());
        if (obj != null) {
            if (StringUtils.isBlank(obj.toString())) {
                return defaultobj;
            }
            return obj;
        } else
            return defaultobj;
    }

    public String getString(String key) {
        Object obj = get(key);
        if (obj != null)
            return obj.toString();
        return null;
    }

    public Integer getInteger(String key) {
        Object obj = get(key);
        if (obj != null && !"".equals(obj.toString()))
            return Integer.parseInt(obj.toString());
        return null;
    }

    public int getInt(String key) {
        Object obj = get(key);
        if (obj != null && !"".equals(obj.toString()))
            return Integer.parseInt(obj.toString());
        return 0;
    }

    public Long getLong(String key) {
        Object obj = get(key);
        if (obj != null && !"".equals(obj.toString()))
            return Long.parseLong(obj.toString());
        return null;
    }

    public long getlong(String key) {
        Object obj = get(key);
        if (obj != null && !"".equals(obj.toString()))
            return Long.parseLong(obj.toString());
        return 0;
    }

    public Double getDoubler(String key) {
        Object obj = get(key);
        if (obj != null && !"".equals(obj.toString()))
            return Double.parseDouble(obj.toString());
        return null;
    }

    public double getDouble(String key) {
        Object obj = get(key);
        if (obj != null && !"".equals(obj.toString()))
            return Double.parseDouble(obj.toString());
        return 0d;
    }

    public double getDouble(String key, String format) {
        Object obj = get(key, format);
        if (obj != null && !"".equals(obj.toString()))
            return Double.parseDouble(obj.toString());
        return 0d;
    }

    public BigDecimal getBigDecimal(String key) {
        Object obj = get(key);
        if (obj != null && !"".equals(obj.toString()))
            return new BigDecimal(obj.toString());
        return null;
    }

    public ArrayList getArray(String key) {
        Object obj = get(key);
        if (obj != null)
            return (ArrayList) obj;
        return null;
    }

    public void remove(String key) {
        dataMap.remove(key.toLowerCase());
    }

    public void clear() {
        dataMap.clear();
    }

    public Map getDataMap() {
        return dataMap;
    }

}
